<template>
    <n-form
        ref="formRef"
        label-placement="top"
        :label-width="80"
        :model="form"
        :rules="rules"
    >
        <n-form-item label="请输入名称" path="name">
            <n-input v-model:value="form.name" placeholder="" />
        </n-form-item>
        <n-form-item label="回复类型">
            <jb-radio-group
                v-model:value="form.replyType"
                url="/api/admin/wechatAutoReply/replyTypes"
                value-to-number
            ></jb-radio-group>
        </n-form-item>
    </n-form>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import type { FormInst, FormRules } from 'naive-ui'
import { Rules } from '@/utils'
import { JBoltApi } from '@/utils/request'
import { useResetableData } from '@/hooks/common/use-resetable-data'
import { ResData } from '@/typings/request'

const props = withDefaults(
    defineProps<{
        id?: string
        mpId:string
    }>(),
    {
        id: '',
        mpId:''
    }
)

/** 表单相关 start */
const formRef = ref<FormInst | null>()
interface BucketType {
    name: string
    replyType: number
}
const form = useResetableData<BucketType>({
    name: '',
    replyType: 1
})
const rules: FormRules = {
    replyType: new Rules().required('请选择回复类型').value,
    name: new Rules().required('请输入名称').value
}
/**
 * 提交表单
 */
async function submit() {
    await formRef.value?.validate()

    let url = props.id ? `/api/admin/wechatAutoReply/update/${props.mpId}-2` : `/api/admin/wechatAutoReply/save/${props.mpId}-2`
    await JBoltApi.tryPost(url, {name: form.name, replyType: form.replyType,type: '2',mpId:props.mpId})

    return true
}

function loadEditData() {
    JBoltApi.get<ResData>(`/api/admin/wechatAutoReply/${props.mpId}-2-${props.id}`).then(({ error, result }) => {
        if (error) return
        form._reset(result.data)
    })
}

onMounted(() => {
    if (props.id) {
        loadEditData()
    }
    console.log('公众号id', props.mpId,props.id)

})

defineExpose({
    submit
})

/** 表单相关 end */
</script>
<style scoped></style>
